<?php

require_once ROOT_PATH . '/lib/confs/Conf.php';
require_once ROOT_PATH . '/lib/dao/DMLFunctions.php';
require_once ROOT_PATH . '/lib/dao/SQLQBuilder.php';
require_once ROOT_PATH . '/lib/common/CommonFunctions.php';
require_once ROOT_PATH . '/lib/common/UniqueIDGenerator.php';
require_once ROOT_PATH . '/lib/common/SearchObject.php';
require_once ROOT_PATH . '/lib/models/hrfunct/EmpInfo.php';


class PAResults {

	const TABLE_NAME = 'hs_hr_pa_results';

	const DB_FIELD_ID = 'id';
	const DB_FIELD_EMP_CODE = 'emp_code';
	const DB_FIELD_PA_TYPE = 'pa_type';
	const DB_FIELD_CURRENT_LEVEL = 'cur_level';
	const DB_FIELD_NEW_LEVEL = 'new_level';
	const DB_FIELD_EXPECT_LEVEL = 'expect_level';
	const DB_FIELD_CURRENT_NET_SALARY = 'cur_net_salary';
	const DB_FIELD_CURRENT_GROSS_SALARY = 'cur_gross_salary';
	const DB_FIELD_CURRENT_CONTRACT_SALARY = 'cur_sal_contract';
	const DB_FIELD_NEW_CONTRACT_SALARY = 'new_sal_contract';
	const DB_FIELD_PA_ADJUST = 'pa_adjust';
	const DB_FIELD_LEVEL_ADJUST = 'level_adjust';
	const DB_FIELD_NEW_NET_SALARY = 'new_net_salary';
	const DB_FIELD_NEW_GROSS_SALARY = 'new_gross_salary';
	const DB_FIELD_PERFORMANCE_ON_JOB = 'per_on_job';
	const DB_FIELD_TECHNICAL_JOB_KNOWLEDGE = 'tec_job_know';
	const DB_FIELD_TEAM_WORK = 'tw_to';
	const DB_FIELD_MANAGEMENT = 'management';
	const DB_FIELD_DISCIPLINED = 'disciplined';
	const DB_FIELD_COMUNICATION = 'communication';
	const DB_FIELD_CORPORATE_DEVELOPMENT = 'cor_dev';
	const DB_FIELD_ENGLISH = 'eng_pa';
	const DB_FIELD_ENGLISH_PRACTICAL = 'eng_pra';
	const DB_FIELD_ENGLISH_TEST = 'eng_test';
	const DB_FIELD_PA_SCORE = 'pa_score';
	const DB_FIELD_PA_CREATED_DATE = 'created_date';

	private $pa_id;
	private $pa_emp_code;
	private $pa_type;
	private $pa_cur_level;
	private $pa_new_level;
	private $pa_expect_level;
	private $pa_cur_net_salary;
	private $pa_cur_gross_salary;
	private $pa_cur_sal_contract;
	private $pa_new_sal_contract;
	private $pa_adjust;
	private $pa_level_adjust;
	private $pa_new_net_salary;
	private $pa_new_gross_salary;
	private $pa_per_on_job;
	private $pa_tec_job_know;
	private $pa_tw_to;
	private $pa_management;
	private $pa_disciplined;
	private $pa_communication;
	private $pa_cor_dev;
	private $pa_eng_pa;
	private $pa_eng_pra;
	private $pa_eng_test;
	private $pa_score;
	private $pa_created_date;

	//Extensiion field
	private $empName;

	public function __construct($id = null) {
		$this->pa_id = $id;
	}

	public function setId($id) {
		$this->pa_id = $id;
	}

	public function getId() {
		return $this->pa_id;
	}

	public function setEmployeeCode($pa_emp_code) {
		$this->pa_emp_code = $pa_emp_code;
	}

	public function getEmployeeCode() {
		return $this->pa_emp_code;
	}

	public function setPAType($paType){
		$this->pa_type= $paType;
	}

	public function getPAType(){
		return $this->pa_type;
	}

	public function setCurrentLevel($currLevel){
		$this->pa_cur_level= $currLevel;
	}

	public function getCurrentLevel(){
		return $this->pa_cur_level;
	}

	public function setNewLevel($newLevel){
		$this->pa_new_level = $newLevel;
	}

	public function getNewLevel(){
		return $this->pa_new_level;
	}

	public function setExpectLevel($expLevel){
		$this->pa_expect_level = $expLevel;
	}

	public function getExpectLevel(){
		return $this->pa_expect_level;
	}

	public function setCurrentNetSalary($currNetSalary){
		$this->pa_cur_net_salary = $currNetSalary;
	}

	public function getCurrentNetSalary(){
		return $this->pa_cur_net_salary;
	}

	public function setCurrentGrossSalary($currGrossSalary){
		$this->pa_cur_gross_salary = $currGrossSalary;
	}

	public function getCurrentGrossSalary(){
		return $this->pa_cur_gross_salary;
	}

	public function setCurrentContractSalary($currContractSalary){
		$this->pa_cur_sal_contract = $currContractSalary;
	}

	public function getCurrentContractSalary(){
		return $this->pa_cur_sal_contract;
	}

	public function setNewContractSalary($newContractSalary){
		$this->pa_new_sal_contract = $newContractSalary;
	}

	public function getNewContractSalary(){
		return $this->pa_new_sal_contract;
	}

	public function setPAAdjust($paAdjust){
		$this->pa_adjust = $paAdjust;
	}

	public function getPAAdjust(){
		return $this->pa_adjust;
	}

	public function setPALevelAdjust($levelAdjust){
		$this->pa_level_adjust = $levelAdjust;
	}

	public function getPALevelAdjust(){
		return $this->pa_level_adjust;
	}

	public function setNewNetSalary($newNetSalary){
		$this->pa_new_net_salary = $newNetSalary;
	}

	public function getNewNetSalary(){
		return $this->pa_new_net_salary;
	}

	public function setNewGrossSalary($newGrossSalary){
		$this->pa_new_gross_salary = $newGrossSalary;
	}

	public function getNewGrossSalary(){
		return $this->pa_new_gross_salary;
	}

	public function setPerformanceOnJob($per_on_job){
		$this->pa_per_on_job = $per_on_job;
	}

	public function getPerformanceOnJob(){
		return $this->pa_per_on_job;
	}

	public function setJobKnowledge($jobKnowledge){
		$this->pa_tec_job_know = $jobKnowledge;
	}

	public function getJobKnowledge(){
		return $this->pa_tec_job_know;
	}

	public function setTeamWork($teamWork){
		$this->pa_tw_to = $teamWork;
	}

	public function getTeamWork(){
		return $this->pa_tw_to;
	}

	public function setManagement($management){
		$this->pa_management = $management;
	}

	public function getManagement(){
		return $this->pa_management;
	}

	public function setDisciplined($disciplined){
		$this->pa_disciplined = $disciplined;
	}

	public function getDisciplined(){
		return $this->pa_disciplined;
	}

	public function setCommunication($communication){
		$this->pa_communication = $communication;
	}

	public function getCommunication(){
		return $this->pa_communication;
	}

	public function setCorporateDevelopment($corpDev){
		$this->pa_cor_dev = $corpDev;
	}

	public function getCorporateDevelopment(){
		return $this->pa_cor_dev;
	}

	public function setPAEnglish($paEnglish){
		$this->pa_eng_pa = $paEnglish;
	}

	public function getPAEngLish(){
		return $this->pa_eng_pa;
	}

	public function setPracticalEnglish($praEnglish){
		$this->pa_eng_pra = $praEnglish;
	}

	public function getPracticalEnglish(){
		return $this->pa_eng_pra;
	}

	public function setTestEnglish($testEnglish){
		$this->pa_eng_test = $testEnglish;
	}

	public function getTestEnglish(){
		return pa_eng_test;
	}

	public function setPAScore($score){
		$this->pa_score = $score;
	}

	public function getPAScore(){
		return $this->pa_score;
	}

	public function setCreatedDate($date){
		$this->pa_created_date = $date;
	}

	public function getCreatedDate(){
		return $this->pa_created_date;
	}
	//Ext field

	public function setEmpName($empName){
		$this->empName = $empName;
	}

	public function getEmpName(){
		return $this->empName;
	}

	public function getPAResultByEmpCode($empCode){
		$selectCondition[0] = "(". self::DB_FIELD_EMP_CODE ." = '". trim($empCode)."' )";
		$paResults = self::_getList($selectCondition);
		if(count($paResults)>0){
			return $paResults[0];
		}else{
			return new PAResults();
		}
	}

	public function getPAResultByEmpID($empID){
		$selectCondition[0] = "(hs_hr_employee.emp_number = '". trim($empID)."' )";
		$paResults = self::_getList($selectCondition);
		if(count($paResults)>0){
			return $paResults[0];
		}else{
			return new PAResults();
		}
	}
	public function save() {
		if (isset($this->pa_id)) {
			return $this->_update();
		} else {
			return $this->_insert();
		}
    }

	public static function delete($ids) {

		$count = 0;
		if (!is_array($ids)) {
			throw new PAException("Invalid parameter to delete(): ids should be an array", PAException::INVALID_PARAMETER);
		}
		foreach ($ids as $id) {
			if (!CommonFunctions::isValidId($id)) {
				throw new PAException("Invalid parameter to delete(): id = $id", PAException::INVALID_PARAMETER);
			}
		}
		if (!empty($ids)) {

			$sql = sprintf("DELETE FROM %s WHERE %s IN (%s)", self::TABLE_NAME,
			                self::DB_FIELD_SKILL_EXPERTISE_ID, implode(",", $ids));
			//print_r($sql);
			$conn = new DMLFunctions();
			$result = $conn->executeQuery($sql);
			if ($result) {
				$count = mysql_affected_rows();
			}
		}
		return $count;

	}

	public static function getAll() {
		return self::_getList();
	}

	private static function _getList($selectCondition = null,$selectOrderBy=self::DB_FIELD_EMP_CODE,$selectOrder=null, $selectLimit=null) {
		$fields[0] = self::DB_FIELD_ID;
		$fields[1] = self::DB_FIELD_EMP_CODE;
		$fields[2] = self::DB_FIELD_PA_TYPE;
		$fields[3] = self::DB_FIELD_CURRENT_LEVEL;
		$fields[4] = self::DB_FIELD_NEW_LEVEL;
		$fields[5] = self::DB_FIELD_EXPECT_LEVEL;
		$fields[6] = self::DB_FIELD_CURRENT_NET_SALARY;
		$fields[7] = self::DB_FIELD_CURRENT_GROSS_SALARY;
		$fields[8] = self::DB_FIELD_CURRENT_CONTRACT_SALARY;
		$fields[9] = self::DB_FIELD_NEW_CONTRACT_SALARY;
		$fields[10] = self::DB_FIELD_PA_ADJUST;
		$fields[11] = self::DB_FIELD_LEVEL_ADJUST;
		$fields[12] = self::DB_FIELD_NEW_NET_SALARY;
		$fields[13] = self::DB_FIELD_NEW_GROSS_SALARY;
		$fields[14] = self::DB_FIELD_PERFORMANCE_ON_JOB;
		$fields[15] = self::DB_FIELD_TECHNICAL_JOB_KNOWLEDGE;
		$fields[16] = self::DB_FIELD_TEAM_WORK;
		$fields[17] = self::DB_FIELD_MANAGEMENT;
		$fields[18] = self::DB_FIELD_DISCIPLINED;
		$fields[19] = self::DB_FIELD_COMUNICATION;
		$fields[20] = self::DB_FIELD_CORPORATE_DEVELOPMENT;
		$fields[21] = self::DB_FIELD_ENGLISH;
		$fields[22] = self::DB_FIELD_ENGLISH_PRACTICAL;
		$fields[23] = self::DB_FIELD_ENGLISH_TEST;
		$fields[24] = self::DB_FIELD_PA_SCORE;
		$fields[25] = "concat(hs_hr_employee.emp_lastname,' ',hs_hr_employee.emp_middle_name,' ',hs_hr_employee.emp_firstname)";
		$fields[26] = self::DB_FIELD_PA_CREATED_DATE;
		$fields[27] = "hs_hr_employee.emp_status";

		$tables[0] = 'hs_hr_pa_results LEFT JOIN hs_hr_employee ON hs_hr_pa_results.emp_code = hs_hr_employee.employee_id' ;
		if ($selectCondition == null) {
			$selectCondition = array();
			$selectCondition[] = "(hs_hr_employee.emp_status !='EST000')"; 
		}
		$sqlBuilder = new SQLQBuilder();
		$sqlBuilder->table_name = self::TABLE_NAME;
		$sql = $sqlBuilder->simpleSelect($tables[0],$fields,$selectCondition,$selectOrderBy,$selectOrder, $selectLimit);
		//die($sql);
		$actList = array();

		$conn = new DMLFunctions();
		$result = $conn->executeQuery($sql);

while ($result && ($row = mysql_fetch_array($result,MYSQL_NUM))) {
			$actList[] = self::_createFromRow($row);
		}
		return $actList;
	}

	private function _insert() {
		$fields[0] = self::DB_FIELD_EMP_CODE;
		$fields[1] = self::DB_FIELD_PA_TYPE;
		$fields[2] = self::DB_FIELD_CURRENT_LEVEL;
		$fields[3] = self::DB_FIELD_NEW_LEVEL;
		$fields[4] = self::DB_FIELD_EXPECT_LEVEL;
		$fields[5] = self::DB_FIELD_CURRENT_NET_SALARY;
		$fields[6] = self::DB_FIELD_CURRENT_GROSS_SALARY;
		$fields[7] = self::DB_FIELD_CURRENT_CONTRACT_SALARY;
		$fields[8] = self::DB_FIELD_NEW_CONTRACT_SALARY;
		$fields[9] = self::DB_FIELD_PA_ADJUST;
		$fields[10] = self::DB_FIELD_LEVEL_ADJUST;
		$fields[11] = self::DB_FIELD_NEW_NET_SALARY;
		$fields[12] = self::DB_FIELD_NEW_GROSS_SALARY;
		$fields[13] = self::DB_FIELD_PERFORMANCE_ON_JOB;
		$fields[14] = self::DB_FIELD_TECHNICAL_JOB_KNOWLEDGE;
		$fields[15] = self::DB_FIELD_TEAM_WORK;
		$fields[16] = self::DB_FIELD_MANAGEMENT;
		$fields[17] = self::DB_FIELD_DISCIPLINED;
		$fields[18] = self::DB_FIELD_COMUNICATION;
		$fields[19] = self::DB_FIELD_CORPORATE_DEVELOPMENT;
		$fields[20] = self::DB_FIELD_ENGLISH;
		$fields[21] = self::DB_FIELD_ENGLISH_PRACTICAL;
		$fields[22] = self::DB_FIELD_ENGLISH_TEST;
		$fields[23] = self::DB_FIELD_PA_SCORE;
		$fields[24] = self::DB_FIELD_PA_CREATED_DATE;

		$values[0] = "'{$this->pa_emp_code}'";
		$values[1] = "'{$this->pa_type}'";
		$values[2] = "'{$this->pa_cur_level}'";
		$values[3] = "'{$this->pa_new_level}'";
		$values[4] = "'{$this->pa_expect_level}'";
		$values[5] = "'{$this->pa_cur_net_salary}'";
		$values[6] = "'{$this->pa_cur_gross_salary}'";
		$values[7] = "'{$this->pa_cur_sal_contract}'";
		$values[8] = "'{$this->pa_new_sal_contract}'";
		$values[9] = "'{$this->pa_adjust}'";
		$values[10] = "'{$this->pa_level_adjust}'";
		$values[11] = "'{$this->pa_new_net_salary}'";
		$values[12] = "'{$this->pa_new_gross_salary}'";
		$values[13] = "'{$this->pa_per_on_job}'";
		$values[14] = "'{$this->pa_tec_job_know}'";
		$values[15] = "'{$this->pa_tw_to}'";
		$values[16] = "'{$this->pa_management}'";
		$values[17] = "'{$this->pa_disciplined}'";
		$values[18] = "'{$this->pa_communication}'";
		$values[19] = "'{$this->pa_cor_dev}'";
		$values[20] = "'{$this->pa_eng_pa}'";
		$values[21] = "'{$this->pa_eng_pra}'";
		$values[22] = "'{$this->pa_eng_test}'";
		$values[23] = "'{$this->pa_score}'";
		$date = date('Y-m-d');
		$values[24] = $date;



		$sqlBuilder = new SQLQBuilder();
		$sqlBuilder->table_name = self::TABLE_NAME;
		$sqlBuilder->flg_insert = 'true';
		$sqlBuilder->arr_insert = $values;
		$sqlBuilder->arr_insertfield = $fields;

		$sql = $sqlBuilder->addNewRecordFeature2();

		$conn = new DMLFunctions();

		$result = $conn->executeQuery($sql);
		if (!$result || (mysql_affected_rows() != 1)) {
			throw new PAException("Insert failed. ", OptionsException::DB_ERROR);
		}

		return $this->pa_id;
	}


	private function _update() {

		$fields[0] = self::DB_FIELD_ID;
		$fields[1] = self::DB_FIELD_EMP_CODE;
		$fields[2] = self::DB_FIELD_PA_TYPE;
		$fields[3] = self::DB_FIELD_CURRENT_LEVEL;
		$fields[4] = self::DB_FIELD_NEW_LEVEL;
		$fields[5] = self::DB_FIELD_EXPECT_LEVEL;
		$fields[6] = self::DB_FIELD_CURRENT_NET_SALARY;
		$fields[7] = self::DB_FIELD_CURRENT_GROSS_SALARY;
		$fields[8] = self::DB_FIELD_CURRENT_CONTRACT_SALARY;
		$fields[9] = self::DB_FIELD_NEW_CONTRACT_SALARY;
		$fields[10] = self::DB_FIELD_PA_ADJUST;
		$fields[11] = self::DB_FIELD_LEVEL_ADJUST;
		$fields[12] = self::DB_FIELD_NEW_NET_SALARY;
		$fields[13] = self::DB_FIELD_NEW_GROSS_SALARY;
		$fields[14] = self::DB_FIELD_PERFORMANCE_ON_JOB;
		$fields[15] = self::DB_FIELD_TECHNICAL_JOB_KNOWLEDGE;
		$fields[16] = self::DB_FIELD_TEAM_WORK;
		$fields[17] = self::DB_FIELD_MANAGEMENT;
		$fields[18] = self::DB_FIELD_DISCIPLINED;
		$fields[19] = self::DB_FIELD_COMUNICATION;
		$fields[20] = self::DB_FIELD_CORPORATE_DEVELOPMENT;
		$fields[21] = self::DB_FIELD_ENGLISH;
		$fields[22] = self::DB_FIELD_ENGLISH_PRACTICAL;
		$fields[23] = self::DB_FIELD_ENGLISH_TEST;
		$fields[24] = self::DB_FIELD_PA_SCORE;
		$fields[25] = self::DB_FIELD_PA_CREATED_DATE;

		$values[0] = $this->pa_id;
		$values[1] = "'{$this->pa_emp_code}'";
		$values[2] = "'{$this->pa_type}'";
		$values[3] = "'{$this->pa_cur_level}'";
		$values[4] = "'{$this->pa_new_level}'";
		$values[5] = "'{$this->pa_expect_level}'";
		$values[6] = "'{$this->pa_cur_net_salary}'";
		$values[7] = "'{$this->pa_cur_gross_salary}'";
		$values[8] = "'{$this->pa_cur_sal_contract}'";
		$values[9] = "'{$this->pa_new_sal_contract}'";
		$values[10] = "'{$this->pa_adjust}'";
		$values[11] = "'{$this->pa_level_adjust}'";
		$values[12] = "'{$this->pa_new_net_salary}'";
		$values[13] = "'{$this->pa_new_gross_salary}'";
		$values[14] = "'{$this->pa_per_on_job}'";
		$values[15] = "'{$this->pa_tec_job_know}'";
		$values[16] = "'{$this->pa_tw_to}'";
		$values[17] = "'{$this->pa_management}'";
		$values[18] = "'{$this->pa_disciplined}'";
		$values[19] = "'{$this->pa_communication}'";
		$values[20] = "'{$this->pa_cor_dev}'";
		$values[21] = "'{$this->pa_eng_pa}'";
		$values[22] = "'{$this->pa_eng_pra}'";
		$values[23] = "'{$this->pa_eng_test}'";
		$values[24] = "'{$this->pa_score}'";
		$date = date('Y-m-d');
		$values[25] = $date;

		$sqlBuilder = new SQLQBuilder();
		$sqlBuilder->table_name = self::TABLE_NAME;
		$sqlBuilder->flg_update = 'true';
		$sqlBuilder->arr_update = $fields;
		$sqlBuilder->arr_updateRecList = $values;

		$sql = $sqlBuilder->addUpdateRecord1(0);

		$conn = new DMLFunctions();
		$result = $conn->executeQuery($sql);

		if (!$result) {
			throw new PAException("Update failed. SQL=$sql", PAException::DB_ERROR);
		}
		return $this->pa_id;
	}

    private static function _createFromRow($row) {

    	$paResults = new PAResults($row[0]);
    	$paResults->setId($row[0]);
    	$paResults->setEmployeeCode($row[1]);
    	$paResults->setPAType($row[2]);
    	$paResults->setCurrentLevel($row[3]);
    	$paResults->setNewLevel($row[4]);
    	$paResults->setExpectLevel($row[5]);
    	$paResults->setCurrentNetSalary($row[6]);
    	$paResults->setCurrentGrossSalary($row[7]);
    	$paResults->setCurrentContractSalary($row[8]);
    	$paResults->setNewContractSalary($row[9]);
    	$paResults->setPAAdjust($row[10]);
    	$paResults->setPALevelAdjust($row[11]);
    	$paResults->setNewNetSalary($row[12]);
    	$paResults->setNewGrossSalary($row[13]);
    	$paResults->setPerformanceOnJob($row[14]);
    	$paResults->setJobKnowledge($row[15]);
    	$paResults->setTeamWork($row[16]);
    	$paResults->setManagement($row[17]);
    	$paResults->setDisciplined($row[18]);
    	$paResults->setCommunication($row[19]);
    	$paResults->setCorporateDevelopment($row[20]);
    	$paResults->setPAEnglish($row[21]);
    	$paResults->setPracticalEnglish($row[22]);
    	$paResults->setTestEnglish($row[23]);
    	$paResults->setPAScore($row[24]);
    	$paResults->setEmpName($row[25]);
    	$paResults->setCreatedDate($row[26]);
    	return $paResults;
    }
}

class PAException extends Exception {
	const INVALID_PARAMETER = 0;
	const DB_ERROR = 1;
}
?>

